package net.expectx.micro.shop.controller.product;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.micro.shop.common.AdminConstants;
import net.expectx.micro.shop.common.AdminResult;
import net.expectx.micro.shop.common.util.ExpectXUtil;
import net.expectx.micro.shop.dao.bean.ProductBrand;
import net.expectx.micro.shop.dao.bean.ProductBrandExample;
import net.expectx.micro.shop.dao.bean.ProductProduct;
import net.expectx.micro.shop.dao.bean.ProductProductExample;
import net.expectx.micro.shop.rpc.api.ProductBrandService;
import net.expectx.micro.shop.rpc.api.ProductProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author lijian
 */
@Controller
@RequestMapping(value = "/manager/productProduct")
public class ProductProductController {
    private static final Integer ZERO=0;
    @Autowired
    private ProductProductService productProductService;
    @Autowired
    private ProductBrandService productBrandService;

    @ApiOperation(value = "分类主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index( HttpServletRequest request) {
        return "product/product/index";
    }

    @ApiOperation(value = "新增分类")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        ProductBrandExample productBrandExample = new ProductBrandExample();
        productBrandExample.createCriteria().andIsDeletedEqualTo(AdminConstants.NO);
        List<ProductBrand> productBrands = productBrandService.selectByExample(productBrandExample);
        modelMap.put("productBrands",productBrands);
        return "product/product/modify";
    }
    @ApiOperation(value = "修改分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        ProductProduct productProduct = productProductService.selectByPrimaryKey(id);
        modelMap.put("productProduct", productProduct);
        ProductBrandExample productBrandExample = new ProductBrandExample();
        productBrandExample.createCriteria().andIsDeletedEqualTo(AdminConstants.NO);
        List<ProductBrand> productBrands = productBrandService.selectByExample(productBrandExample);
        modelMap.put("productBrands",productBrands);
        return "product/product/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String productProductName,String productSn,Long productCategoryId) {
        Map<String, Object> result = new HashMap<>(16);
        ProductProductExample productProductExample = new ProductProductExample();

        /**
         * 创造条件
         */
        ProductProductExample.Criteria criteria = productProductExample.createCriteria();

        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        if (StringUtils.isNotBlank(productProductName)){
            criteria.andProductNameLike("%"+productProductName+"%");
        }
        if (StringUtils.isNotBlank(productSn)){
            criteria.andProductSnEqualTo(productSn);
        }
        if (productCategoryId!=null){
            criteria.andProductCategoryIdEqualTo(productCategoryId);
        }
        /**
         * 分页
         */
        productProductExample.setPageIndex(page);
        productProductExample.setPageSize(limit);
        /**
         * 排序
         */
        productProductExample.setOrderByClause("orders desc");

        List<ProductProduct> productProducts = productProductService.selectByExample(productProductExample);

        long total = productProductService.countByExample(productProductExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", productProducts);
        return result;
    }

    @ApiOperation(value = "新增分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ProductProduct productProduct) {
        productProduct.setGmtCreate(System.currentTimeMillis());
        productProduct.setGmtModified(System.currentTimeMillis());
        productProduct.setIsDeleted(AdminConstants.NO);
        productProduct.setOrders(System.currentTimeMillis());
        productProduct.setProductClickCount(0L);
        productProduct.setProductIsVariant((byte) 1);
        productProduct.setProductIsDelete((byte) 0);
        BaseResult baseResult = productProductService.insertSelective(productProduct);
        return  baseResult;
    }

    @ApiOperation(value = "修改分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, ProductProduct productProduct) {
        productProduct.setGmtModified(System.currentTimeMillis());
        productProduct.setOrders(System.currentTimeMillis());
        return productProductService.updateByPrimaryKeySelective(productProduct);
    }

    @ApiOperation(value = "删除产品")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult delete(@PathVariable("ids") String ids) {
        ProductProductExample productProductExample = new ProductProductExample();
        productProductExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        return  productProductService.physicalRemoveByExample(AdminConstants.YES, productProductExample);
    }

    @ApiOperation(value = "置顶产品")
    @RequestMapping(value = "/setTop/{id}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult setTop(@PathVariable("id") Long id) {
        ProductProduct productProduct = productProductService.selectByPrimaryKey(id);
        productProduct.setOrders(System.currentTimeMillis());
        return  productProductService.updateByPrimaryKeySelective(productProduct);
    }

}
